package com.zhifei.web.controller.bs;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhifei.common.utils.Constant;
import com.zhifei.common.utils.Result;
import com.zhifei.system.domain.DataSourceEntity;
import com.zhifei.system.domain.dto.DataSourceDTO;
import com.zhifei.system.domain.dto.DeleteDTO;
import com.zhifei.system.domain.vo.DataSourceVo;
import com.zhifei.system.service.DataSourceService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.validation.Valid;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/dataSource")
@Api(tags="数据源管理")
public class DataSourceController {

    @Autowired
    private DataSourceService dataSourceService;

    @PostMapping("/add")
    @ApiOperation("添加数据源")
    public Result<DataSourceEntity> add(@RequestBody @Valid DataSourceDTO dto, BindingResult bindingResult){
        if (bindingResult.hasErrors()) {
            return Result.error(bindingResult.getFieldError().getDefaultMessage());
        }
        return dataSourceService.add(dto);
    }

    @PostMapping("/update")
    @ApiOperation("编辑数据源")
    public Result<DataSourceEntity> update(@RequestBody @Valid DataSourceDTO dto, BindingResult bindingResult){
        if (bindingResult.hasErrors()) {
            return Result.error(bindingResult.getFieldError().getDefaultMessage());
        }
        return dataSourceService.update(dto);
    }

    @PostMapping("/delete")
    @ApiOperation("删除数据源")
    public Result delete(@RequestBody @Valid DeleteDTO dto, BindingResult bindingResult){
        if (bindingResult.hasErrors()) {
            return Result.error(bindingResult.getFieldError().getDefaultMessage());
        }
        dataSourceService.delete(dto);
        return Result.success();
    }

//    @GetMapping("/list")
    @DeleteMapping(value = "/remove/{courseNo}" )
    @ApiOperation("数据源列表")
    public Result<List<DataSourceVo>> list(@PathVariable("courseNo") String courseNo){
        System.out.println("dto:"+courseNo);
        List<DataSourceVo> list = dataSourceService.queryList();
        return Result.success(list);
    }

    @GetMapping("page")
    @ApiOperation("分页数据源列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = Constant.PAGE, value = "当前页码，从1开始", paramType = "query", required = true, dataType="int", defaultValue = "1") ,
            @ApiImplicitParam(name = Constant.LIMIT, value = "每页显示记录数", paramType = "query",required = true, dataType="int", defaultValue = "10")
    })
    public Result<Page<DataSourceVo>> page(@ApiIgnore @RequestParam Map<String, Object> params){
        Page page = dataSourceService.queryPage(params);
        return Result.success(page);
    }

}


